﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using DAL;
using BOL;
using Website.Logger;


namespace UILGestion.Admin
{
    public partial class InsumoReadForm1 : System.Web.UI.Page
    {
        //private static  int idTipoInsumo;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    internalid.Value = (string)Request.QueryString["id"];
                    //idTipoInsumo=int.Parse((string)Request.QueryString["idtipoinsumo"]);
                    menuCtrl1.loadMenuAdmin();
                    FormatForm();
                }
                catch (SqlException sqlex)
                {
                    WebServerSideSqlError.Log(sqlex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                    "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);
                 


                }
                catch (Exception ex)
                {
                    WebServerSideError.Log(ex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                    "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);
                 
                }

            }
        }
        private void readObject()
        {
            dbGestionDataContext _db =
                new dbGestionDataContext(Parametros.strConnString);



            InsumoBL bol= new InsumoBL();

            InsumoInfo  insumo=bol.Load(int.Parse(internalid.Value));


            codigoLabel.Text = insumo.codigo;
            descripcionLabel.Text = insumo.descripcion;
            unidadLabel.Text=insumo.unidad;
            controStockCheckBox.Checked = insumo.bitControlStock;
            revisadoCheckBox.Checked = (insumo.bitRec == null) ? false : insumo.bitRec.Value;

            cuentaLabel.Text = insumo.cuenta;
            grupoLabel.Text=insumo.codigoPadre+" - "+insumo.insumoPadre;
           
            fechaCreLabel.Text=insumo.fechaCre.ToString();
            fechaModLabel.Text = insumo.fechaMod.ToString();
            codigoS10.Text = insumo.codigoS10;
            idPadre.Value = insumo.idPadre.ToString();

            //string tipoinsumo = DbUtil.Db.TipoObra.Single(p => p.idInternal == idTipoInsumo).descripcion;
            subgrupoTabPanel.Visible = false;
            comprasTabPanel.Visible = false;


            if (insumo.nivel==0)
            {
                //clase

                tituloLabel.Text = String.Format("CLASE");
                claseLabel1.Visible = false;
                familiaLabel1.Visible = false;
                claseLabel.Text="";
                familiaLabel.Text="";
                unidadLabel1.Visible = false;
                unidadLabel.Visible = false;
                controStockCheckBox.Visible = false;
                controlLabel.Visible=false;
                subfamiliaLabel1.Visible = false;
                subfamiliaLabel.Visible = false;
                subgrupoTabPanel.Visible = true;
               
          
            }
            if (insumo.nivel==1)
            {
                //familia
                tituloLabel.Text = String.Format("FAMILIA");
                claseLabel1.Visible = true;
                
                familiaLabel1.Visible = false;
                claseLabel.Text = insumo.codigoPadre + "-" +insumo.insumoPadre;
                familiaLabel.Text="";

                unidadLabel1.Visible = false;
                unidadLabel.Visible = false;
               
                controStockCheckBox.Visible = false;
                controlLabel.Visible = false;
                subfamiliaLabel1.Visible = false;
                subfamiliaLabel.Visible = false;
                subgrupoTabPanel.Visible = true;
               
            }

            if(insumo.nivel==2)
            {
                tituloLabel.Text = String.Format("SUB FAMILIA");
                InsumoInfo insumoFamila = bol.Load(insumo.idPadre.Value);

                claseLabel1.Visible = true;
                familiaLabel1.Visible = true;
                
                familiaLabel.Text=insumo.codigoPadre+ "-" + insumo.insumoPadre;
                claseLabel.Text = insumoFamila.codigoPadre + "-"+insumoFamila.insumoPadre;

                subfamiliaLabel1.Visible = false;
                subfamiliaLabel.Visible = false;

                unidadLabel1.Visible = false;
                unidadLabel.Visible = false;
                controStockCheckBox.Visible = false;
      
                controlLabel.Visible = false;
               
                subgrupoTabPanel.Visible = true;
            }
            if (insumo.nivel == 3)
            {
                tituloLabel.Text = String.Format("INSUMO");
                InsumoInfo insumosubfamilia = bol.Load(insumo.idPadre.Value);
                InsumoInfo insumofamilia = bol.Load(insumosubfamilia.idPadre.Value);


                subfamiliaLabel.Text = insumosubfamilia.codigo + "-" + insumosubfamilia.descripcion;
                familiaLabel.Text = insumofamilia.codigo + "-" + insumofamilia.descripcion;
                claseLabel.Text = insumofamilia.codigoPadre + "-" + insumofamilia.insumoPadre;
                revisadoLabel1.Visible = false;
                revisadoCheckBox.Visible = false;
                cuentaLabel.Visible = false;
                cuentaLabel1.Visible = false;
                comprasTabPanel.Visible = true;
            }

        }
        private void FormatForm()
        {
            //--------------------
            int idEmpleadoEmisor = int.Parse(System.Web.HttpContext.Current.Session["idEmpleado"].ToString());
            bool acceso = new DAL.FormularioAcceso().VerificarAcceso(DbUtil.Db,
                idEmpleadoEmisor,
                10, //Insumo
                1); //lectura
            //------------------------- 
            // los paneles presentes en el formulario
            pnlMain.Visible = true;
            PanelButtom.Visible = true;
            Panelcab.Visible = true;

            //ShowAllComboBox();
            //restricionesForm();
               //         string Lang = "es-ES";  //para controlar el idioma 
   //         System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo
   //(Lang);

            readObject();


        }

        protected void refreshButton_Click(object sender, EventArgs e)
        {

            try
            {
                FormatForm();
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                  "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }

        protected void EditarButton_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("InsumoInsertForm.aspx?id=" + internalid.Value+"&type=E", false);
               
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                  "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }

        }

        protected void EliminarButton_Click(object sender, EventArgs e)
        {
            try
            {
                InsumoBL insumo = new InsumoBL();
                bool result = insumo.delete(int.Parse(internalid.Value));
                if (result)
                {
                    Response.Redirect("InsumoListForm.aspx", false);
                }
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }

        protected void nuevoButton_Click(object sender, EventArgs e)
        {
            try
            {
                Response.Redirect("InsumoInsertForm.aspx?type=A", true);
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }

        protected void anteriorButton_Click(object sender, EventArgs e)
        {
            try
            {
                DAL.Insumo insumo = new DAL.Insumo();


                int idAnterior = insumo.getidAnterior(DbUtil.Db, int.Parse(internalid.Value));

                Response.Redirect("InsumoReadForm.aspx?id=" + idAnterior, true);
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }

        protected void siguienteButton_Click(object sender, EventArgs e)
        {
            try
            {
                DAL.Insumo insumo = new DAL.Insumo();
                int idSiguiente=insumo.getidSiguiente(DbUtil.Db, int.Parse(internalid.Value));

                Response.Redirect("InsumoReadForm.aspx?id=" + idSiguiente , true);

              
               
            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }

        protected void grupoLabel_Click(object sender, EventArgs e)
        {
            try
            {
                if (idPadre.Value!="")
                    Response.Redirect("InsumoReadForm.aspx?id=" + idPadre.Value , true);
               

            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                  "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }
        }
    }
}